package com.example.controller;

import com.example.entity.SpuComment;
import com.example.service.SpuCommentService;
import com.example.vo.IdHolder;
import com.example.vo.Result;
import com.example.vo.comment.PageResult;
import com.example.vo.comment.SpuCommentDash;
import com.example.vo.comment.SpuCommentVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description:
 * @Author: aimi
 * @Create: 2024-05-16
 **/
@RestController
@Slf4j
@Tag(name = "商品评论接口")
public class SpuCommentController {
    @Resource
    private SpuCommentService commentService;

    // sort=0表示根据点赞数排序，=1表示根据评论时间排序
    @GetMapping("/buyer/common/getSpuCommentList")
    @Operation(summary = "获取商品评论")
    public Result<PageResult<SpuCommentVo>> getBookCommentList(Long spuId,
                                                               @RequestParam(defaultValue = "1") Integer pageNo,
                                                               @RequestParam(defaultValue = "0") Integer sortType,
                                                               @RequestParam(defaultValue = "-1") Integer type) {
        log.info("获取spu id为{}的第{}页评论,type={}", spuId, pageNo,type);
        List<SpuCommentVo> commentVoList = commentService.getSpuCommentList(spuId,pageNo,sortType,type);
        Long total = commentService.getCommentCount(spuId);
        PageResult<SpuCommentVo> pageResult = new PageResult<>(total, commentVoList);
        return Result.ok(pageResult);
    }

    @PostMapping("/buyer/comment/addSpuComment")
    @Operation(summary = "发布评论")
    public Result addComment(@RequestBody SpuComment spuComment){
        log.info("发布评论{}",spuComment);
        return commentService.addComment(spuComment);
    }

    @Operation(summary = "获取用户已点赞的评论列表")
    @GetMapping("/buyer/comment/getLikeIds")
    public Result<List<Long>> getLikeIds(@RequestParam Long spuId){
        log.info("获取用户已点赞的评论列表：{}",spuId);
        List<Long> likeIds = commentService.getLikeIds(spuId);
        return Result.ok(likeIds);
    }

    @Operation(summary = "点赞或者取消点赞")
    @PostMapping("/buyer/comment/likeOrCancelLike")
    public Result likeOrCancelLike( Long commentId){
        log.info("点赞或者取消点赞：{}",commentId);
        commentService.likeOrCancelLike(commentId);
        return Result.ok();
    }

    @Operation(summary = "获取评论dash参数")
    @GetMapping("/buyer/common/comment/getSpuCommentDash")
    public Result<SpuCommentDash> getSpuCommentDash(Long spuId){
        SpuCommentDash dash = commentService.getSpuCommentDash(spuId);
        return Result.ok(dash);
    }

    @DeleteMapping("/buyer/comment/{id}")
    @Operation(summary = "删除评论")
    public Result<Long> deleteComment(@PathVariable Integer id){
        log.info("删除评论：{}",id);
        commentService.deleteComment(id);
        return Result.ok(IdHolder.getUserId());
    }

    @PostMapping("/buyer/common/comment/upload")
    public Result<String> uploadCommentImg(MultipartFile file){
        String url = commentService.upload(file);
        return Result.ok(url);
    }
}